package com.gopay.checkorder.service.impl.expay;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.checkorder.service.task.BankCountDownLatch;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.YeePayConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.domain.bank.yeepay.expay.YeePayExPayMessage;
import com.gopay.common.domain.cashier.BankCardType;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;
import com.gopay.common.util.TransUtil;
import com.gopay.remote.bank.ExPayInteractRemoteService;

/**
 * 易宝快捷支付结果查询
 * 
 * @author zhengxue
 * 
 */
public class CheckOrderYeePayExPayImpl implements BankOrderService {
	private final static Logger logger = LoggerFactory.getLogger("YEEPAY");
	@Resource(name = "bankExPayClient")
	private ExPayInteractRemoteService bankExPayClient;

	@Override
	public CheckOrderResult checkOrder(BankOrder bankOrder, String arg1) {
		CheckOrderResult checkOrderResult = new CheckOrderResult();
		checkOrderResult.setSuccess(false);
		checkOrderResult.setOrderId(bankOrder.getOrderId());
		
		YeePayExPayMessage message = new YeePayExPayMessage();
		message.setOrderId(bankOrder.getOrderId()); // 国付宝系统订单号
		message.setBankOrderId(bankOrder.getOutOrderId());
		message.setBank(BankConstants.BANK_LOGO_YEEPAY);
	    message.setBankIntrChann(BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode()));
	    message.setBankBusProd(BisBankBusinessProd.get(bankOrder.getBankBizProdCode()));
		message.setBank(bankOrder.getBankCode());
		
		Result result = new Result();
		try {
			result = bankExPayClient.queryOrder(message);
			if (result == null || !result.isSuccess()) {
				logger.error("bankExPayClient fail.");
				if(result ==null || ProcCodeConstants.PROC_CODE_100E6058.equals(result.getResultCode())){
					BankCountDownLatch.failIncrease(bankOrder.getBankCode());
				}				
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
				checkOrderResult.setErrMsg("系统返回对账结果为空");
				return checkOrderResult;
			}
			
			YeePayExPayMessage res = (YeePayExPayMessage) result.getDefaultModel();
			if (res == null) {
				// 查询结果为空
				logger.error("fail:orderId:" + bankOrder.getOrderId()+ " errcode:" + ProcCodeConstants.PROC_CODE_100E6022+ " err:bis query is null.");
				checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
				checkOrderResult.setErrMsg("银行返回对账结果为空");
				return checkOrderResult;
			}
			
			if(res.isFlag()){
				if (YeePayConstants.YEEPAY_EXPAY_QUERY_SUCCESS.equals(res
						.getStatus())) {
					checkOrderResult.setSuccess(true);
					InAccInfo accInfo = new InAccInfo();
					accInfo.setOrderId(bankOrder.getOrderId());
					String amt = TransUtil.amountConvert2Yuan(res.getAmount().toString());
					accInfo.setAmount(new BigDecimal(amt));
					accInfo.setRetCode("00");
					accInfo.setCardType(BankCardType.Unknown.toString());
					checkOrderResult.setAccInfo(accInfo);
					logger.error("易宝快捷支付订单：" + bankOrder.getOrderId() + "的交易成功" + "返回码：" + res.getStatus());
				} else if (YeePayConstants.YEEPAY_EXPAY_QUERY_FAIL
						.equals(res.getStatus())) {
				    InAccInfo accInfo = new InAccInfo();
                    accInfo.setOrderId(bankOrder.getOrderId());
                    String amt = TransUtil.amountConvert2Yuan(res.getAmount().toString());
                    accInfo.setAmount(new BigDecimal(amt));
                    accInfo.setRetCode("01");
                    accInfo.setCardType(BankCardType.Unknown.toString());
                    checkOrderResult.setAccInfo(accInfo);
					checkOrderResult.setBankFailure(true);
					checkOrderResult.setErrMsg("银行返回交易失败");
					logger.error("易宝快捷支付订单：" + bankOrder.getOrderId() + "的交易失败"+ "返回码：" + res.getErrorcode() + "返回信息：" + res.getErrormsg());
				} else if (YeePayConstants.YEEPAY_EXPAY_QUERY_DEALING
						.equals(res.getStatus())) {
					checkOrderResult.setErrMsg("银行订单正在处理中");
					logger.error("易宝快捷支付订单：" + bankOrder.getOrderId() + "的交易处理中"+ "返回码：" + res.getStatus());
				} else if (YeePayConstants.YEEPAY_EXPAY_QUERY_UNTREATED
						.equals(res.getStatus())) {
					checkOrderResult.setErrMsg("银行订单未处理");
					logger.error("易宝快捷支付订单：" + bankOrder.getOrderId() + "的交易未处理"+ "返回码：" + res.getStatus());
				} else {
					checkOrderResult.setErrMsg("银行返回交易状态不明");
					logger.error("易宝快捷支付订单：" + bankOrder.getOrderId() + "返回交易状态不明，系统暂不处理"+ "返回码：" + res.getStatus());
				}	
			} else {
				checkOrderResult.setErrMsg("银行返回验签失败");
				logger.error("易宝快捷支付订单：" + bankOrder.getOrderId() + "验签失败"+ "Flag：" + res.isFlag());
			}
			return checkOrderResult;
		} catch (Exception e) {
			logger.error("bankExPayClient fail.", e);
			checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
			checkOrderResult.setErrMsg("易宝快捷支付对账异常");
			return checkOrderResult;
		}
	}

	@Override
	public BankOrder getDbOrder() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BaseMessage dayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result getDayendMsg(String string) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CheckOrderResult> checkDayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CheckOrderResult checkDayendOrder(BaseDayendEntry bdm, BankOrder bo) {
		// TODO Auto-generated method stub
		return null;
	}

}
